<?php
/**
 * Plugin Name: 微信公众平台-文章同步
 * Description: 将文章单向同步至微信公众平台永久素材库
 * Version: 0.0.1
 * Author: xiaomlove
 */

add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
    add_options_page( '微信公众平台设置', '文章同步', 'manage_options', 'wechat-official-account', 'my_options_page' );
}
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {

    /*
       * http://codex.wordpress.org/Function_Reference/register_setting
       * register_setting( $option_group, $option_name, $sanitize_callback );
       * The second argument ($option_name) is the option name. It’s the one we use with functions like get_option() and update_option()
       * */
    # With input validation:
    # register_setting( 'my-settings-group', 'wechat_official_account_options', 'my_settings_validate_and_sanitize' );
    register_setting( 'my-settings-group', 'wechat_official_account_options' );

    /*
   * http://codex.wordpress.org/Function_Reference/add_settings_section
   * add_settings_section( $id, $title, $callback, $page );
   * */
    add_settings_section( 'section-1', 'APPID & APPKEY', 'section_1_callback', 'my-plugin' );
//    add_settings_section( 'section-2', __( 'Section Two', 'textdomain' ), 'section_2_callback', 'my-plugin' );

    /*
     * http://codex.wordpress.org/Function_Reference/add_settings_field
     * add_settings_field( $id, $title, $callback, $page, $section, $args );
     * */
    add_settings_field( 'field-1-1', 'APPID', 'field_1_1_callback', 'my-plugin', 'section-1' );
    add_settings_field( 'field-1-2', 'APPKEY', 'field_1_2_callback', 'my-plugin', 'section-1' );

//    add_settings_field( 'field-2-1', __( 'Field One', 'textdomain' ), 'field_2_1_callback', 'my-plugin', 'section-2' );
//    add_settings_field( 'field-2-2', __( 'Field Two', 'textdomain' ), 'field_2_2_callback', 'my-plugin', 'section-2' );

}
/*
 * THE ACTUAL PAGE
 * */
function my_options_page() {
    ?>
    <div class="wrap">
        <h2>微信公众号设置</h2>
        <form action="options.php" method="POST">
            <?php settings_fields('my-settings-group'); ?>
            <?php do_settings_sections('my-plugin'); ?>
            <?php submit_button(); ?>
        </form>
    </div>
<?php }
/*
* THE SECTIONS
* Hint: You can omit using add_settings_field() and instead
* directly put the input fields into the sections.
* */
function section_1_callback() {
    _e( '将目标公众号的id及key填写至此');
}
function section_2_callback() {
    _e( 'Some help text regarding Section Two goes here.', 'textdomain' );
}
/*
* THE FIELDS
* */
function field_1_1_callback() {

    $settings = (array) get_option( 'wechat_official_account_options' );
    $field = "id";
    $value = isset($settings[$field]) ? esc_attr( $settings[$field] ) : "";

    echo "<input type='text' name='wechat_official_account_options[$field]' value='$value' class='regular-text' />";
}
function field_1_2_callback() {

    $settings = (array) get_option( 'wechat_official_account_options' );
    $field = "key";
    $value = isset($settings[$field]) ? esc_attr( $settings[$field] ) : "";

    echo "<input type='text' name='wechat_official_account_options[$field]' value='$value' class='regular-text' />";
}
function field_2_1_callback() {

    $settings = (array) get_option( 'wechat_official_account_options' );
    $field = "field_2_1";
    $value = esc_attr( $settings[$field] );

    echo "<input type='text' name='wechat_official_account_options[$field]' value='$value' />";
}
function field_2_2_callback() {

    $settings = (array) get_option( 'wechat_official_account_options' );
    $field = "field_2_2";
    $value = esc_attr( $settings[$field] );

    echo "<input type='text' name='wechat_official_account_options[$field]' value='$value' />";
}
/*
* INPUT VALIDATION:
* */
function my_settings_validate_and_sanitize( $input ) {
    $settings = (array) get_option( 'wechat_official_account_options' );

    if ( $some_condition == $input['field_1_1'] ) {
        $output['field_1_1'] = $input['field_1_1'];
    } else {
        add_settings_error( 'wechat_official_account_options', 'invalid-field_1_1', 'You have entered an invalid value into Field One.' );
    }

    if ( $some_condition == $input['field_1_2'] ) {
        $output['field_1_2'] = $input['field_1_2'];
    } else {
        add_settings_error( 'wechat_official_account_options', 'invalid-field_1_2', 'You have entered an invalid value into Field One.' );
    }

    // and so on for each field

    return $output;
}

function plugin_add_settings_link( $links ) {
    $settings_link = '<a href="options-general.php?page=wechat-official-account">' . __( 'Settings' ) . '</a>';
    array_push( $links, $settings_link );
    return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'plugin_add_settings_link' );


add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 );

function modify_list_row_actions($actions, $post)
{
    // Build your links URL.
    $url = admin_url( 'admin.php?page=mycpt_page&post=' . $post->ID );

    $actions['push-to-wechat-official-account'] = sprintf('<a href="%s">同步至微信公众平台</a>', $url);

    return $actions;
}